<?php
namespace app\mobile\controller;

use app\model\Fee; 
use app\model\Comm; 
use app\model\Theme; 
use app\model\Order; 

class Fahongbao extends Base
{
    public function Fahongbao()
    {
global $_G, $_P;
// $fans = $this->checkuser();
$system    = $_G['system'];
$op        = !empty($_P['op']) ? $_P['op'] : 'display';
$pm = array(":uniacid"=>$_G['mpid']);

$themeid = intval($_P['themeid']);
$theme = Theme::getThemeById($themeid);
if($op == 'comm'){
    if($system['autored']== 0 || $system['autored']== 3){
        $result['errcode'] = 1;
        $result['msg'] = 'autored is clsesd';
        return_msg($result, '', 'ajax');
    }

    $totalpayfee = Order::getRealByThemeIdnew($themeid) + $theme['chongzhi'];
    if ($totalpayfee<1){
        $result['errcode'] = 1;
        $result['msg'] = '您的余额不足,无法发红包';
        return_msg($result, '', 'ajax');
    }
    $row = Comm::getUnPayOneItemNew($system,$themeid);
    $r = array();
    if(!empty($row)){
        //$cachekey = 'comm'.$_G['mpid'].MODULE_NAME.$row['id'];
        //$settings = cache_load($cachekey);
        //if(empty($settings)){
        //    $settings = "commid:".$row['id'];
        //     cache_write($cachekey, $settings);
            $r = Fee::commRecord($row,$system);
        //}
        //更新红包排行记录

    }
    $result['errcode'] = 0;
    return_msg($result, '', 'ajax');
}



if($op == 'check'){
    if($system['autored']!= 1 && $system['autored']!= 2){
        $result['errcode'] = 1;
        $result['msg'] = 'autored is clsesd';
        return_msg($result, '', 'ajax');
    }
    $list = db_select( "SELECT * FROM " . tablename (t_comm) . " WHERE uniacid= :uniacid and status = 0 and type < 4 order by id desc" ,$pm);
    foreach ($list as $index =>$row){
        $r = Fee::commRecord($row,$system);
    }
    $result['errcode'] = 0;
    $result['list'] = $list;
    return_msg($result, '', 'ajax');
}


}
}